package com.budwk.app.sqfwV2.generator.dayingRoadExcel;

import com.alibaba.fastjson.JSONObject;
import com.budwk.app.sqfw.constants.CommonConstants;
import com.budwk.app.sqfw.constants.DictConstants;
import com.budwk.app.sqfw.models.Sqfw_formSummary_new;
import com.budwk.app.sqfwDoc.bo.SqfwExcelForSummaryBO;
import com.budwk.app.sqfwDoc.bo.SqfwExcelJudgeRes;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.BaseTemplateExcelGenerator;
import com.budwk.app.sqfwDoc.codeGenerator.excelGenerate.SqfwExcelCreateCommonBO;
import com.budwk.app.sqfwV2.component.DayingRoadPdfComponent;
import com.budwk.app.sqfwV2.component.ProblemComponent;
import com.budwk.app.sqfwV2.dto.dto.FireSafetyOnDayingRoadPdfDTO;
import com.budwk.app.sqfwV2.dto.dto.centuryCity.SafeCheckTaskParamDataDTO;
import com.budwk.app.sqfwV2.dto.image.FireSafetyOnDayingRoadExcelImageConfigBO;
import com.budwk.app.sqfwV2.enums.DayingRoadFileServiceEnum;
import com.budwk.app.sys.services.SysDictService;
import com.budwk.app.sys.vo.SysDictVO;
import com.budwk.app.util.StrUtil;
import com.budwk.app.util.poiUtils.excelPictureConfigBO.ExcelFileCreateParamBO;
import com.budwk.app.util.poiUtils.nutzBeanUtils.NutzIocUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * 消防安全重大风险隐患专项排查整治附件3/Excel
 */

@Slf4j
public class SafetyRiskRecBookExcelGenerator extends BaseTemplateExcelGenerator implements DayingRoadExcelGenerator {

    @Override
    protected String getTemplateName() {
        return DayingRoadFileServiceEnum.DAYING_ROAD_EXCEL_SPECIAL.getFilePath();
    }

    /**
    * 可以参照 {@link com.budwk.app.sqfw.enums.DownLoadFileNameEnum}
    *
    * @return 文件名名称
    */
    @Override
    public String getDownloadFileName() {
        //TODO 在这里返回你生成的文件名名称，注意注意注意不用带上后缀
        return DayingRoadPdfComponent.getDictName(DayingRoadFileServiceEnum.DAYING_ROAD_EXCEL_SPECIAL.getCode(), null);
    }

    /**
     * 用户自定义excel基础参数，比如表头这样的公用数据
     * @param excelParam 已经填充的基础数据据
     * @param excelCreateParam excel生成提供的原料
     */
    @Override
    protected void customerPopularSheetBaseParam(Map<String, Object> excelParam, SqfwExcelCreateCommonBO excelCreateParam) {
        excelParam.put("district", DayingRoadPdfComponent.getDistrict(excelCreateParam.getBaseExcelParamDTO().getDistrict()));
    }

    @Override
    protected void customerPopularSheetDataParam(List<Map<String, Object>> listMap, SqfwExcelCreateCommonBO excelCreateParam) {
        List<Sqfw_formSummary_new> summaryList = DayingRoadPdfComponent.getSummaryList(listMap, excelCreateParam);
        log.info("SafetyRiskRecBookExcelGenerator customerPopularSheetDataParam summaryList: {}", JSONObject.toJSONString(summaryList));
        int num = 1;
        SysDictService dictService = NutzIocUtils.getBean(SysDictService.class);
        // 获取主管部门
        List<SysDictVO> departDicts = dictService.listDictByCode(DictConstants.COMPETENT_DEPART_TYPES);
        for (Sqfw_formSummary_new summary : summaryList) {
            FireSafetyOnDayingRoadPdfDTO roadPdfDTO = JSONObject.parseObject(summary.getParamData(), FireSafetyOnDayingRoadPdfDTO.class);
            log.info("SafetyRiskRecBookExcelGenerator customerPopularSheetDataParam roadPdfDTO: {}", JSONObject.toJSONString(roadPdfDTO));
            if (!isGenerateExcel(roadPdfDTO)) {
                continue;
            }
            // 构建数据
            Map<String, Object> map = buildMap(roadPdfDTO, departDicts);
            map.put("num", num ++);
            listMap.add(map);
        }
        log.info("SafetyRiskRecBookExcelGenerator customerPopularSheetDataParam end: {}", JSONObject.toJSONString(listMap));
    }

    /**
     * ●生成此表的逻辑为：
     * ○当【a5】状态为【大型商场、超市、酒店】、
     * 【幼儿园、寄宿制学校、社会福利机构】、
     * 【医院、诊所、卫生服务站】、
     * 【劳动密集型企业、多业态混合经营场所】、
     * 【其他重点消防安全场所】五项中任意一项时，生成此表单。
     * @param roadPdfDTO
     * @return
     */
    public boolean isGenerateExcel(FireSafetyOnDayingRoadPdfDTO roadPdfDTO) {
        // 判断a5是否为重点场所 选择重点场所 生成表格
        return DayingRoadPdfComponent.isKeyLocations(roadPdfDTO);
    }

    private Map<String, Object> buildMap(FireSafetyOnDayingRoadPdfDTO roadPdfDTO, List<SysDictVO> departDicts) {
        Map<String, Object> map = new HashMap<>();

        map.put("competentDepartmentName", CommonConstants.HORIAONTAL_LINE);
        if (StringUtils.isNotEmpty(roadPdfDTO.getCompetentDepartment())) {
            Optional<SysDictVO> optional = departDicts.stream().filter(d -> roadPdfDTO.getCompetentDepartment().equals(d.getCode())).findFirst();
            map.put("competentDepartmentName", optional.isPresent() ? optional.get().getName() : roadPdfDTO.getCompetentDepartment());
        }
        map.put("placeName", StrUtil.replaceNullStr(roadPdfDTO.getPlaceName())); // 场所名称
        map.put("address", StrUtil.replaceNullStr(roadPdfDTO.getAddress())); //单位地址
        map.put("nameAndMobile", roadPdfDTO.getOperator() + "：" + roadPdfDTO.getOperatorTelephone());
        // 31个问题集
        for (SafeCheckTaskParamDataDTO keySafe : roadPdfDTO.getKeySafetyIndustries()) {
            map.put(keySafe.getFields(), keySafe.getKey().equals(String.valueOf(true)) ? "是" : "否");
        }
        map.put("otherHiddenDangers", "无"); //具体隐患
        List<SafeCheckTaskParamDataDTO> others = roadPdfDTO.getOtherHiddenDangers();
        if (others != null && others.size() > 0) {
            String rectificationMeasure = others.get(others.size() - 1).getRectificationMeasure();
            map.put("otherHiddenDangers", StringUtils.isEmpty(rectificationMeasure) ? "无" : rectificationMeasure); //具体隐患
        }
        map.put("rectifyingDangersMeasures", StrUtil.replaceNullStr(roadPdfDTO.getRectifyingDangersMeasures())); //隐患整改措施
        // 隐患整改时限
        map.put("rectificationStr", ProblemComponent.getDangerStr(roadPdfDTO, roadPdfDTO.getRectificationDate()));
        // 部门联合执法情况
        map.put("deptJoinLawSituation", StrUtil.replaceNullStr(roadPdfDTO.getDeptJoinLawSituation()));
        return map;
    }

    @Override
    protected void completeExcelFileCreateParam(ExcelFileCreateParamBO excelFileCreateParam) {

    }

    @Override
    protected void customerPopularRowDataMap(Map<String, Object> rowDataMap, SqfwExcelForSummaryBO forSummaryBO) {

    }

    /**
     * 如果在遍历之前就能够判断excel是否生成，那么就在这个方法中进行判断
     * @param excelCreateParam excel生成所需的最为全面的数据
     * @param excelParam excel已经填充的部分数据
     */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeErgodic(SqfwExcelCreateCommonBO excelCreateParam, Map<String, Object> excelParam) {
        return DayingRoadPdfComponent.getSqfwExcelJudgeRes(excelCreateParam, new SafetyRiskRecBookExcelGenerator());
    }


    /**
    * 如果在遍历之后能够判断excel是否生成，那么就在这个方法中进行判断
    * @param excelParam excel已经填充的部分数据
    */
    @Override
    protected SqfwExcelJudgeRes judgeDataIntoExcelBeforeCreate(Map<String, Object> excelParam) {
        return null;
    }

    @Override
    protected Class<?> getImageConfigBO() {
        return FireSafetyOnDayingRoadExcelImageConfigBO.class;
    }
}
